package com.ant.create.service.impl;

import com.ant.common.utils.CreateCodeUtil;
import com.ant.common.utils.StringUtil;
import com.ant.create.entity.ColumnInformation;
import com.ant.create.entity.CreateSet;
import com.ant.create.entity.TableInformation;
import com.ant.create.mapper.CreateFactoryDao;
import com.ant.create.service.CreateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class CreateServiceImpl implements CreateService {

    @Autowired
    private CreateFactoryDao createFactoryDao;

    @Override
    public void createCode(CreateSet createSet) {
        long start = System.currentTimeMillis();
        CreateCodeUtil createCodeUtil = CreateCodeUtil.init(createSet.getCodePackage(), createSet.getTemplates());

        List<String> tableList = StringUtil.strToList(createSet.getTableNames());
        List<TableInformation> tableNameList = createFactoryDao.getTableName(createSet.getDatabaseName(), tableList);

        Map<String, String> packageMap = new HashMap<>();
        packageMap.put("servicePackage", "service");
        packageMap.put("daoPackage", "mapper");
        packageMap.put("domainPackage", "entity");
        packageMap.put("controllerPackage", "controller");
        packageMap.put("allPackage", createSet.getAllPackage());

        for (TableInformation tableInformation : tableNameList) {
            List<ColumnInformation> columnInformationList = createFactoryDao.getColumnName(createSet.getDatabaseName(), tableInformation.getTableName());

            String tableName = tableInformation.getTableName();
            //tableName = tableName.replace("tb_", "");

            createCodeUtil.createCode(tableName, tableInformation.getTableComment(), columnInformationList, packageMap);
        }
        long end = System.currentTimeMillis();
        long diff = end - start;
        log.info("-----------------------end-----------------------用时：" + diff + "毫秒");
    }
}
